package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.yuneec.fly.GlobalParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jw.gu on 2016/6/16.
 */
public class DrawerView extends View {
    private float mX, mY;
    private Path mPath;
    private Paint mPaint;
    private static final float TOUCH_TOLERANCE = 3;
    private Canvas mCanvas;
    public float touchX;
    public float touchY;
    private ArrayList<Point> movePoints;
    private OnDrawFinishListener onDrawFinishListener;

    public void setOnDrawFinishListener(OnDrawFinishListener onDrawFinishListener) {
        this.onDrawFinishListener = onDrawFinishListener;
    }

    public DrawerView(Context context) {
        this(context, null);

    }

    public DrawerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        movePoints = new ArrayList<>();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(AbstractMapView.unFlyedRouteColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(5);

        mCanvas = new Canvas();
        mPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(mPath, mPaint);
        super.onDraw(canvas);
    }

    private void onTouchDown(float x, float y) {
        movePoints.clear();
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void onTouchMove(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
            movePoints.add(new Point((int) x, (int) y));
        }
    }

    private void onTouchUp(float x, float y) {
//        mPath.lineTo(mX, mY);
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        touchX = event.getX();
        touchY = event.getY();
        if (!GlobalParams.addingPoints || GlobalParams.getMissionStaus() == GlobalParams.MissionStatus.WAYPOINT_RUNNING) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onTouchDown(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                onTouchMove(touchX, touchY);
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                calculatePoints();
                onTouchUp(touchX, touchY);
                break;
            default:
                break;
        }
        invalidate();
        return true;
    }

    private void calculatePoints() {
        if (movePoints.size() == 0 || onDrawFinishListener == null) {
            return;
        }
        onDrawFinishListener.onDrawFinish(movePoints);
    }

    public interface OnDrawFinishListener {
        void onDrawFinish(List<Point> points);
    }
}
